<?php
/**
 * @title 427. 建立四叉树
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a QuadTree node.
 * class Node {
 *     public $val = null;
 *     public $isLeaf = null;
 *     public $topLeft = null;
 *     public $topRight = null;
 *     public $bottomLeft = null;
 *     public $bottomRight = null;
 *     function __construct($val, $isLeaf) {
 *         $this->val = $val;
 *         $this->isLeaf = $isLeaf;
 *         $this->topLeft = null;
 *         $this->topRight = null;
 *         $this->bottomLeft = null;
 *         $this->bottomRight = null;
 *     }
 * }
 */

class Solution {
    /**
     * @param Integer[][] $grid
     * @return Node
     */
    function construct($grid) {
        /**
         * @since 2023-12-29 矩阵边长
         */
        $n = count($grid);

        /**
         * @since 2023-12-29 全局矩阵
         */
        $this->grid = $grid;

        /**
         * @since 2023-12-29 创建四叉树
         * @return Node
         */
        return $this->FourTree(0, $n-1, 0, $n-1);
    }

    /**
     * @title 返回在指定范围内，创建四叉树
     * @author start2004
     * @since 2023-12-29 Created
     *
     * @param Integer $rMin
     * @param Integer $rMax
     * @param Integer $cMin
     * @param Integer $cMax
     *
     * @return Node
     */
    function FourTree($rMin, $rMax, $cMin, $cMax){
        /**
         * @since 2023-12-29 这个范围内，所有值是否相同
         */
        $val = $this->grid[$rMin][$cMin];
        // echo "起点:{$rMin},{$cMin}, 终点:{$rMax},{$cMax}, val:{$val}\n";
        for($r=$rMin; $r<=$rMax; $r++){
            for($c=$cMin; $c<=$cMax; $c++){
                if($this->grid[$r][$c] != $val){
                    $val = null;
                    break 2;
                } else {}
            }
        }

        /**
         * @since 2023-12-29 所有值相同
         */
        if($val !== null){
            /**
             * @since 2023-12-29 创建叶子节点
             */
            return new Node($val===1, true);
        } else {}

        /**
         * @since 2023-12-29 中间值
         */
        $rMid = ($rMin+$rMax)>>1;
        $cMid = ($cMin+$cMax)>>1;

        /**
         * @since 2023-12-29 再分一次四叉树
         */
        $node = new Node(true, false);
        $node->topLeft = $this->FourTree($rMin, $rMid, $cMin, $cMid);
        $node->topRight = $this->FourTree($rMin, $rMid, $cMid+1, $cMax);
        $node->bottomLeft = $this->FourTree($rMid+1, $rMax, $cMin, $cMid);
        $node->bottomRight = $this->FourTree($rMid+1, $rMax, $cMid+1, $cMax);

        /**
         * @return Node
         */
        return $node;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.29_8.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
